#!user/bin/env python3
# -- coding: utf-8 --
# 闭包：内部函数引用了外部函数的局部变量，那么内部函数就被认为是闭包
def ad(a, b):
    c = 1

    def add():
        s = a + b + c
        print(f'相加的和为：{s}')

    return add  # 不能加括号，加括号表示调用


a = ad(2, 3)
a()

print(''.center(30, '='))


# 装饰器
# 将被装饰的函数当作参数传递给装饰器对应的函数，并返回包装后的被装饰的函数。
# # 无参数情况
def hanshu1(x):
    def hanshu2():
        print('程序开始运行。。')
        x()
        print('程序运行结束！')

    return hanshu2


@hanshu1  # @语法糖
def hanshu3():
    print('我是张三')


bianliang = hanshu1(hanshu3)
hanshu3()

print(''.center(30, '='))


# 有参数情况
def hanshu1(x):
    def hanshu2(name):
        print('程序运行开始了')
        x(name)
        print('程序运行结束')

    return hanshu2


@hanshu1
def hanshu3(name):
    print(f'我的名字是{name}')


hanshu3('二蛋')
print(''.center(30, '='))


# 收集参数情况
def hanshu1(x):
    def hanshu2(*args):
        print('程序开始')
        x(*args)
        print('程序结束')

    return hanshu2


@hanshu1
def hanshu3(xingshi):
    print(f'{xingshi}老师，您好！')


@hanshu1
def hanshu4(name, age, job):
    print(f'我的名字是{name},我的年龄是{age},我的职业是{job}')


hanshu3('张')
hanshu4('张三', '24', '编程')
print(''.center(30, '='))


# 关键字参数
def hanshu1(xingcan):
    def hanshu2(*args, **kwargs):
        print('程序开始进行:')
        xingcan(*args, **kwargs)
        print('程序已经结束。')

    return hanshu2


@hanshu1
def hanshu3(xingshi):
    print(f'{xingshi}老司机，您好！')


@hanshu1
def hanshu5(name, age, **kwargs):
    print(f'我叫{name}，年龄{age}', kwargs)


hanshu3('刘')
hanshu5('张三丰', '90', job='打太极', hobby='泡脚)')
